package com.pro.h5case.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.h5case.admin.service.AdminService;
import com.pro.h5case.admin.service.LogManageService;
import com.pro.h5case.admin.service.OrderManageService;
import com.pro.h5case.common.model.BAdmin;
import com.pro.h5case.common.model.BDictionary;
import com.pro.h5case.common.model.BOrder;
import com.pro.h5case.common.model.BPlan;
import com.pro.h5case.common.model.LogInfoEntity;
import com.pro.h5case.common.service.BDictionaryService;
import com.pro.h5case.admin.service.PlanManageService;

@Controller
@RequestMapping("/logManage")
public class LogManageController {
	@Resource
	private PlanManageService planManageService;
	@Resource
	private BDictionaryService dictionaryService;
	@Resource
	private AdminService adminService;
	@Resource
	private LogManageService logManageService;
	

	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public String list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			Map<String,Object> parameter = new HashMap<String, Object>();
			
			parameter.put("begin", 0);
			parameter.put("size", 15);
			parameter.put("logOperation", "Plan");
			List<LogInfoEntity> logList = logManageService.selectLogByMap(parameter);
			for (int i = 0; i < logList.size(); i++) {
				LogInfoEntity log = logList.get(i);
				switchOperation(log);
			}
			model.addAttribute("logList", logList);
			int planSum = logManageService.countLogByMap(parameter);
			int pageSum = planSum / 15;
			if(planSum%15 == 0){
				
			}else{
				pageSum+=1;
			}
			model.addAttribute("pageNo", 1);
			model.addAttribute("pageSum", pageSum);
			return "admin/logManage/list";
		
	}
	/**
	 * 翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("pageNo") int pageNo,@RequestParam("key") String key)
			throws Exception {

			Map<String,Object> parameter = new HashMap<String, Object>();
			parameter.clear();
			parameter.put("begin", pageNo*15);
			parameter.put("size", 15);
			parameter.put("sort", key);
			parameter.put("logOperation", "Plan");
			List<LogInfoEntity> logList = logManageService.selectLogByMap(parameter);
			for (int i = 0; i < logList.size(); i++) {
				LogInfoEntity log = logList.get(i);
				switchOperation(log);
			}
			model.addAttribute("logList", logList);
			int planSum = logManageService.countLogByMap(parameter);
			
			int pageSum = planSum / 15;
			if(planSum%15 == 0){
				
			}else{
				pageSum+=1;
			}
			parameter.clear();
			parameter.put("logList", logList);
			parameter.put("pageSum", pageSum);
			parameter.put("pageNo", pageNo);
			return parameter;
		
	}
	private void switchOperation(LogInfoEntity log){
		switch (log.getLogOperation()) {
	/*	case "log in":
			log.setLogOperation("登录");
			break;
		case "log out":
			log.setLogOperation("登出");
			break;*/
		case "insertPlan":
			log.setLogOperation("新增案例");
			getTitle(log);
			break;
		case "updatePlan":
			log.setLogOperation("修改案例");
			getTitle(log);
			break;
		case "deletePlan":
			log.setLogOperation("删除案例");
			getTitle(log);
			break;
		case "offlinePlan":
			log.setLogOperation("案例下线");
			getTitle(log);
			break;
		case "auditPlan":
			log.setLogOperation("审核通过");
			getTitle(log);
			break;
		/*case "insert":
			log.setLogOperation("新增管理员");
			break;
		case "update":
			log.setLogOperation("修改管理员");
			break;
		case "delete":
			log.setLogOperation("删除管理员");
			break;*/
		default:
			break;
		}
	}
	private void getTitle(LogInfoEntity log){
		if (log.getLogContentTitle()==null || "".equals(log.getLogContentTitle()) || " ".equals(log.getLogContentTitle())) {
			BPlan plan = planManageService.selectByPrimaryKey(log.getLogContentId());
			if(plan!=null){
				log.setLogContentTitle(plan.getPlanName());
			}
			
		}

	}
	
}
